package com.tang.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.nio.charset.StandardCharsets;

public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        /*
         * 心跳监测
         * 1、readerIdleTimeSeconds 读超时时间 2s
         * 2、writerIdleTimeSeconds 写超时时间 2s
         * 3、allIdleTimeSeconds    读写超时时间 2s
         * 4、TimeUnit.SECONDS 秒[默认为秒，可以指定]
         */
        ch.pipeline().addLast(new IdleStateHandler(2, 2, 2));
        ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
        ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
        ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
        ch.pipeline().addLast(new ServerHandler());
    }

}
